<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//管理员中心
namespace app\controller;
use think\facade\Db;

class ApiAdminIndex extends ApiAdmin {
	public function index() {
		$where = ['aid' => aid, 'uid' => uid];
		if (bid == 0) {
			$set = Db::name('admin_set')->field('name,logo,desc,tel')->where('aid', aid)->find();
		} else {
			$set = Db::name('business')->field('name,logo,desc,tel,money')->where('id', bid)->find();
		}

		$uinfo = array();
		$uinfo['bid'] = $this->user['bid'];
		$uinfo['un'] = $this->user['un'];
		$uinfo['tmpl_orderconfirm'] = $this->user['tmpl_orderconfirm'];
		$uinfo['tmpl_orderpay'] = $this->user['tmpl_orderpay'];
		$uinfo['tmpl_ordershouhuo'] = $this->user['tmpl_ordershouhuo'];
		$uinfo['tmpl_ordertui'] = $this->user['tmpl_ordertui'];
		$uinfo['tmpl_withdraw'] = $this->user['tmpl_withdraw'];
		$uinfo['tmpl_formsub'] = $this->user['tmpl_formsub'];
		$uinfo['tmpl_kehuzixun'] = $this->user['tmpl_kehuzixun'];
		$uinfo['tmpl_recharge'] = $this->user['tmpl_recharge'];

		if ($this->user['isadmin'] == 0) {
			$uinfo['tmpl_orderconfirm_show'] = 0;
			$uinfo['tmpl_orderpay_show'] = 0;
			$uinfo['tmpl_ordershouhuo_show'] = 0;
			$uinfo['tmpl_ordertui_show'] = 0;
			$uinfo['tmpl_withdraw_show'] = 0;
			$uinfo['tmpl_formsub_show'] = 0;
			$uinfo['tmpl_kehuzixun_show'] = 0;
			$uinfo['tmpl_recharge_show'] = 0;
			$notice_auth_data = json_decode($this->user['notice_auth_data'], true);
			foreach ($notice_auth_data as $v) {
				$uinfo[$v . '_show'] = 1;
			}
		} else {
			$uinfo['tmpl_orderconfirm_show'] = 1;
			$uinfo['tmpl_orderpay_show'] = 1;
			$uinfo['tmpl_ordershouhuo_show'] = 1;
			$uinfo['tmpl_ordertui_show'] = 1;
			$uinfo['tmpl_withdraw_show'] = 1;
			$uinfo['tmpl_formsub_show'] = 1;
			$uinfo['tmpl_kehuzixun_show'] = 1;
			$uinfo['tmpl_recharge_show'] = 1;
		}
		$where = [];
		$where['aid'] = aid;
		$where['bid'] = bid;
		if ($this->user['mdid']) {
			$where['mdid'] = $this->user['mdid'];
		}
		$count0_service = 0 + Db::name('yuyue_order')->where($where)->where('status', 0)->count();
		$count1_service = 0 + Db::name('yuyue_order')->where($where)->where('status', 1)->count();
		$count2_service = 0 + Db::name('yuyue_order')->where($where)->where('status', 2)->count();
		$count3_service = 0 + Db::name('yuyue_order')->where($where)->where('status', 3)->count();

		$count0 = 0 + Db::name('shop_order')->where($where)->where('status', 0)->count();
		$count1 = 0 + Db::name('shop_order')->where($where)->where('status', 1)->count();
		$count2 = 0 + Db::name('shop_order')->where($where)->where('status', 2)->count();
		$count3 = 0 + Db::name('shop_order')->where($where)->where('status', 3)->count();

		$count4 = 0 + Db::name('shop_refund_order')->where($where)->where('refund_status', 'in', [1, 4])->count();

		$collageCount = 0 + Db::name('collage_order')->where($where)->count();
		$luckycollageCount = 0 + Db::name('lucky_collage_order')->where($where)->count();
		$kanjiaCount = 0 + Db::name('kanjia_order')->where($where)->count();
		$seckillCount = 0 + Db::name('seckill_order')->where($where)->count();
		$scoreshopCount = 0 + Db::name('scoreshop_order')->where($where)->count();
		$yuyueorderCount = 0 + Db::name('yuyue_order')->where(['aid' => aid, 'bid' => bid])->count();
		$tuangouCount = 0 + Db::name('tuangou_order')->where($where)->count();
		$cycleCount = 0 + Db::name('cycle_order')->where($where)->count();
		$todayMoney = Db::name('payorder')->where('aid', aid)->where('bid', bid)->where('status', 1)->where('paytime', '>', strtotime(date("Y-m-d")))->sum('money');

		$productCount = Db::name('shop_product')->where(['aid' => aid, 'bid' => bid])->count();
		$formlogCount = Db::name('form_order')->where(['aid' => aid, 'bid' => bid])->count();

		$hxwehere = [];
		$hxwehere['order.aid'] = aid;
		$hxwehere['order.bid'] = bid;
		if ($this->user['mdid']) {
			$hxwehere['order.uid'] = $this->user['id'];
		}
		$hexiaoCount = Db::name('hexiao_order')->alias('order')->field('member.nickname,member.headimg,order.*')->join('member member', 'member.id=order.mid')->where($hxwehere)->count();

		//$hexiaoCount = Db::name('hexiao_order')->where($hxwehere)->count();
		$mdwhere = [];
		$mdwhere[] = ['maidan_order.aid', '=', aid];
		$mdwhere[] = ['maidan_order.bid', '=', bid];
		$mdwhere[] = ['maidan_order.status', '=', 1];
		if ($this->user['mdid']) {
			$mdwhere[] = ['maidan_order.mdid', '=', $this->user['mdid']];
		}
		$maidanCount = Db::name('maidan_order')->alias('maidan_order')->field('member.nickname,member.headimg,maidan_order.*')->join('member member', 'member.id=maidan_order.mid', 'left')->where($mdwhere)->count();

		if ($this->user['auth_type'] == 0) {
			$auth_data = json_decode($this->user['auth_data'], true);
			$auth_path = [];
			foreach ($auth_data as $v) {
				$auth_path = array_merge($auth_path, explode(',', $v));
			}
			$auth_data = $auth_path;
		} else {
			$auth_data = 'all';
		}
		$showcollageorder = false;
		$showkanjiaorder = false;
		$showseckillorder = false;
		$showscoreshoporder = false;
		$showluckycollageorder = false;
		$showyuyueorder = false;
		$showtuangouorder = false;
		$showformlog = false;
		$showshoporder = false;
		$showyuekeorder = false;
		$showcycleorder = false;
		if ($auth_data == 'all' || in_array('ShopOrder/*', $auth_data)) {
			$showshoporder = true;
		}
		if ($auth_data == 'all' || in_array('CollageOrder/*', $auth_data)) {
			$showcollageorder = true;
		}
		if ($auth_data == 'all' || in_array('KanjiaOrder/*', $auth_data)) {
			$showkanjiaorder = true;
		}
		if ($auth_data == 'all' || in_array('SeckillOrder/*', $auth_data)) {
			$showseckillorder = true;
		}
		if ($auth_data == 'all' || in_array('ScoreshopOrder/*', $auth_data)) {
			$showscoreshoporder = true;
		}
		if ($auth_data == 'all' || in_array('YuyueOrder/*', $auth_data)) {
			$showyuyueorder = true;
		}
		if ($auth_data == 'all' || in_array('LuckyCollageOrder/*', $auth_data)) {
			$showluckycollageorder = true;
		}
		if ($auth_data == 'all' || in_array('TuangouOrder/*', $auth_data)) {
			$showtuangouorder = true;
		}
		if ($auth_data == 'all' || in_array('Form/*', $auth_data)) {
			$showformlog = true;
		}
		if ($auth_data == 'all' || in_array('Maidan/*', $auth_data)) {
			$showmaidanlog = true;
		}
		if ($auth_data == 'all' || in_array('CycleOrder/*', $auth_data)) {
			$showcycleorder = true;
		}
		$showshortvideo = false;
		$showrecharge = false;
		if (bid > 0 && getcustom('business_withdraw')) {
			$showrecharge = true;
		}

		$showworkorder = false;
		if (in_array('WorkorderCategory/*', $auth_data)) {
			$showworkorder = true;
		}
		$showworkadd = false;
		$workordercount = 0;
		$wxtmplset = Db::name('wx_tmplset')->where('aid', aid)->find();
		$tmpl_orderconfirmId = $wxtmplset['tmpl_orderconfirm'];
		$uinfo['tmpl_orderconfirmNum'] = Db::name('member_tmplnum')->where('mid', mid)->where('tmplid', $wxtmplset['tmpl_orderconfirm'])->value('num');
		$uinfo['tmpl_ordershouhuoNum'] = Db::name('member_tmplnum')->where('mid', mid)->where('tmplid', $wxtmplset['tmpl_ordershouhuo'])->value('num');
		$uinfo['tmpl_ordertuiNum'] = Db::name('member_tmplnum')->where('mid', mid)->where('tmplid', $wxtmplset['tmpl_ordertui'])->value('num');
		$uinfo['tmpl_withdrawNum'] = Db::name('member_tmplnum')->where('mid', mid)->where('tmplid', $wxtmplset['tmpl_withdraw'])->value('num');
		$uinfo['tmpl_kehuzixunNum'] = Db::name('member_tmplnum')->where('mid', mid)->where('tmplid', $wxtmplset['tmpl_kehuzixun'])->value('num');

		if (!$uinfo['tmpl_orderconfirmNum']) {
			$uinfo['tmpl_orderconfirmNum'] = 0;
		}

		if (!$uinfo['tmpl_ordershouhuoNum']) {
			$uinfo['tmpl_ordershouhuoNum'] = 0;
		}

		if (!$uinfo['tmpl_ordertuiNum']) {
			$uinfo['tmpl_ordertuiNum'] = 0;
		}

		if (!$uinfo['tmpl_withdrawNum']) {
			$uinfo['tmpl_withdrawNum'] = 0;
		}

		if (!$uinfo['tmpl_kehuzixunNum']) {
			$uinfo['tmpl_kehuzixunNum'] = 0;
		}

		$rdata = [];
		$rdata['status'] = 1;
		$rdata['set'] = $set;
		$rdata['wxtmplset'] = $wxtmplset;
		$rdata['uinfo'] = $uinfo;
		$rdata['count0_service'] = $count0_service;
		$rdata['count1_service'] = $count1_service;
		$rdata['count2_service'] = $count2_service;
		$rdata['count3_service'] = $count3_service;

		$rdata['count0'] = $count0;
		$rdata['count1'] = $count1;
		$rdata['count2'] = $count2;
		$rdata['count3'] = $count3;
		$rdata['count4'] = $count4;
		$rdata['collageCount'] = $collageCount;
		$rdata['luckycollageCount'] = $luckycollageCount;
		$rdata['kanjiaCount'] = $kanjiaCount;
		$rdata['seckillCount'] = $seckillCount;
		$rdata['tuangouCount'] = $tuangouCount;
		$rdata['scoreshopCount'] = $scoreshopCount;
		$rdata['maidanCount'] = $maidanCount;
		$rdata['productCount'] = $productCount;
		$rdata['yuyueorderCount'] = $yuyueorderCount;
		$rdata['hexiaoCount'] = $hexiaoCount;
		$rdata['cycleCount'] = $cycleCount;

		$rdata['formlogCount'] = $formlogCount;
		$rdata['todayMoney'] = $todayMoney;
		$rdata['workordercount'] = $workordercount;
		$rdata['auth_data'] = $this->auth_data;
		$rdata['showbusinessqr'] = (getcustom('plug_businessqr') && bid > 0 ? true : false);
		$rdata['showzhaopin'] = (getcustom('zhaopin') ? true : false);
		$rdata['showshoporder'] = $showshoporder;
		$rdata['showcollageorder'] = $showcollageorder;
		$rdata['showCycleorder'] = $showcycleorder;

		$rdata['showkanjiaorder'] = $showkanjiaorder;
		$rdata['showseckillorder'] = $showseckillorder;
		$rdata['showscoreshoporder'] = $showscoreshoporder;
		$rdata['showluckycollageorder'] = $showluckycollageorder;
		$rdata['showtuangouorder'] = $showtuangouorder;
		$rdata['showmaidanlog'] = $showmaidanlog;
		$rdata['showformlog'] = $showformlog;
		$rdata['showworkadd'] = $showworkadd;
		$rdata['showworkorder'] = $showworkorder;
		$rdata['showshortvideo'] = $showshortvideo;
		$rdata['showyuyueorder'] = $showyuyueorder;
		$rdata['showyuekeorder'] = $showyuekeorder;
		//商家充值
		$rdata['showrecharge'] = $showrecharge;

		$searchmember = false;
		$rdata['searchmember'] = $searchmember;

		return $this->json($rdata);
	}
	//管理员登录
	public function login() {
		if (request()->isPost()) {
			$username = trim(input('post.username'));
			$password = trim(input('post.password'));
			$captcha = trim(input('post.captcha'));
			if ($username == '' || $password == '') {
				return $this->json(['status' => 0, 'msg' => '用户名和密码不能为空']);
			} elseif ($captcha == '') {
				return $this->json(['status' => 0, 'msg' => '验证码不能为空']);
			} elseif (strtolower($captcha) != strtolower(cache($this->sessionid . '_captcha'))) {
				return $this->json(['status' => 0, 'msg' => '验证码错误']);
			}
			$rs = db('admin_user')->where(['un' => $username, 'pwd' => md5($password), 'aid' => aid])->find();
			if ($rs) {
				$aid = $rs['aid'];
				if ($rs['status'] != 1) {
					return ['status' => 0, 'msg' => '账号未启用'];
				}

				cookie("AM_AID", $aid, 86400 * 300);
				cookie("AM_UID_{$aid}", $rs['id'], 86400 * 300);
				cookie("AM_TOKEN_{$aid}", md5($username . 'wxx1$%^' . $password . '_@#$x'), 86400 * 300);
				session("AM_UID_{$aid}", $rs['id']);

				$update = [];
				$update['ip'] = request()->ip();
				$update['logintime'] = time();
				if (!$rs['mid']) {
					//账号没有绑定会员ID
					$rs2 = db('admin_user')->where('mid', mid)->find();
					if (!$rs2) {
						//该会员没有绑定其他账号
						$update['mid'] = mid;
					}
				}
				db('admin_user')->where(['id' => $rs['id']])->update($update);
				db('admin_loginlog')->insert(['aid' => $aid, 'uid' => $rs['id'], 'logintime' => time(), 'loginip' => request()->ip(), 'logintype' => '手机端登录']);
				cache($this->sessionid . '_uid', $rs['id']);
				return $this->json(['status' => 1, 'msg' => '登录成功']);
			} else {
				cache($this->sessionid . '_captcha', null);
				return $this->json(['status' => 2, 'msg' => '账号或密码错误']);
			}
		} else {
			cache($this->sessionid . '_uid', null);
			return $this->json(['status' => 1]);
		}
	}
	//消息通知开关
	public function setusertmpl() {
		$post = input('post.');
		if (in_array($post['field'], ['tmpl_orderconfirm', 'tmpl_orderpay', 'tmpl_ordershouhuo', 'tmpl_withdraw', 'tmpl_formsub', 'tmpl_kehuzixun', 'tmpl_recharge', 'tmpl_ordertui'])) {
			Db::name('admin_user')->where('id', uid)->update([$post['field'] => $post['value']]);
		}
		return $this->json(['status' => 1, 'msg' => '设置成功']);
	}
	//修改密码
	public function setpwd() {
		if (request()->isPost()) {
			$user = Db::name('admin_user')->where('id', uid)->find();
			$oldpwd = input('post.oldpwd');
			$pwd = input('post.pwd');
			if (md5($oldpwd) != $user['pwd']) {
				return $this->json(['status' => 0, 'msg' => '原密码输入错误']);
			}
			Db::name('admin_user')->where('id', $user['id'])->update(['pwd' => md5($pwd)]);
			return $this->json(['status' => 1, 'msg' => '修改成功']);
		}
		$user = Db::name('admin_user')->field('id,un')->where('id', uid)->find();
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['user'] = $user;
		return $this->json($rdata);
	}
	//商家推广码
	public function getbusinessqr() {
		$posterset = Db::name('plug_businessqr_poster')->where('aid', aid)->where('bid', bid)->order('id')->find();
		if (!$posterset) {
			return json(['status' => -4, 'msg' => '请先在电脑端后台设置推广码']);
		}
		$rdata = [];
		$rdata['posterurl'] = $posterset['posterurl'];
		return $this->json($rdata);
	}
	//设置店铺信息
	public function setinfo() {
		if (request()->isPost()) {
			$postinfo = input('post.info/a');
			$info = [];
			$info['tel'] = $postinfo['tel'];
			$info['logo'] = $postinfo['logo'];
			$info['desc'] = $postinfo['desc'];
			$info['pics'] = $postinfo['pics'];
			$info['content'] = $postinfo['content'];
			$info['address'] = $postinfo['address'];
			$info['longitude'] = $postinfo['longitude'];
			$info['latitude'] = $postinfo['latitude'];
			$info['weixin'] = $postinfo['weixin'];
			$info['aliaccount'] = $postinfo['aliaccount'];
			$info['bankname'] = $postinfo['bankname'];
			$info['bankcarduser'] = $postinfo['bankcarduser'];
			$info['bankcardnum'] = $postinfo['bankcardnum'];
			$info['start_hours'] = $postinfo['start_hours'];
			$info['end_hours'] = $postinfo['end_hours'];
//			$info['invoice'] = $postinfo['invoice'];
			//			$info['invoice_type'] = implode(',',$postinfo['invoice_type']);
			$info['is_open'] = $postinfo['is_open'];
			$info['autocollecthour'] = $postinfo['autocollecthour'];
			$info['start_hours2'] = $postinfo['start_hours2'];
			$info['end_hours2'] = $postinfo['end_hours2'];
			$info['start_hours3'] = $postinfo['start_hours3'];
			$info['end_hours3'] = $postinfo['end_hours3'];
			db('business')->where(['aid' => aid, 'id' => bid])->update($info);
			\app\common\System::plog('系统设置');
			return json(['status' => 1, 'msg' => '设置成功', 'url' => true]);
		}
		$info = Db::name('business')->where('aid', aid)->where('id', bid)->find();
		$rdata = [];
		$rdata['info'] = $info;
		return $this->json($rdata);
	}

	//获取商家余额
	public function getBusiness() {
		$where = ['aid' => aid, 'uid' => uid];
		if (bid == 0) {
			$set = Db::name('admin_set')->field('name,logo,desc,tel')->where('aid', aid)->find();
		} else {
			$set = Db::name('business')->field('name,logo,desc,tel,money')->where('id', bid)->find();
		}
		return json(['status' => 1, 'data' => $set]);
	}
	//充值
	public function recharge() {
		$bid = bid;
		$money = floatval(input('post.money'));
		if ($money == 0) {
			return json(['status' => 0, 'msg' => '请输入充值金额']);
		}
		if (request()->isPost()) {
			if ($money > 0) {
				$ordernum = date('ymdHis') . aid . rand(1000, 9999);
				//增加消费记录
				$orderdata = [];
				$orderdata['aid'] = aid;
				$orderdata['bid'] = bid;
				$orderdata['createtime'] = time();
				$orderdata['money'] = $money;
				$orderdata['ordernum'] = $ordernum;
				$orderid = Db::name('business_recharge_order')->insertGetId($orderdata);
				$payorderid = \app\model\Payorder::createorder(aid, $orderdata['bid'], '0', 'business_recharge', $orderid, $ordernum, t('余额') . '充值', $money);

				return $this->json(['status' => 1, 'msg' => '提交成功', 'orderid' => $orderid, 'payorderid' => $payorderid]);
			} else {
				return $this->json(['status' => 0, 'msg' => '充值金额必须大于0']);
			}
		}
	}
}